﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class Admin_OtherInfoEdit : System.Web.UI.Page
{
    public int News_id;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["admin"] == null)
        {
            Response.Redirect("../Default.aspx");
        }
        else
        {
            PublishLabel.Text = "显示";
            List.Text = Session["List"].ToString();
            News_Edit.Text = Session["NewsType"].ToString();
            if (Session["OperationType"].ToString().Equals("New"))
            {
                if (0 == (int)Session["GroupID"])
                {
                    PublishLabel.Visible = false;
                    cbPublish.Visible = false;
                }
            }
            else if (Session["OperationType"].ToString().Equals("Edit"))
            {
                News_id = Convert.ToInt32(Session["News_id"]);
                NewsDataClassesDataContext dc = new NewsDataClassesDataContext();
                var OtherInfos = from otherinfo in dc.OtherInfos
                                 where otherinfo.OtherInfo_id.Equals(News_id)
                           select new
                           {
                               标题 = otherinfo.Title,
                               内容 = otherinfo.Content,
                               发布 = otherinfo.isPublished
                           };

                foreach (var otherinfo in OtherInfos)
                {
                    content.Value = Convert.ToString(otherinfo.内容);
                    title.Value = Convert.ToString(otherinfo.标题);
                    if (otherinfo.发布 == 1) cbPublish.Checked = true;
                }
                if (0 == (int)Session["GroupID"])
                {
                    PublishLabel.Visible = false;
                    cbPublish.Visible = false;
                }
                dc.Dispose();
            }
        }
    }
    protected void Submit_Button_Click(object sender, EventArgs e)
    {
        string tableTitle = "ctl00$ContentPlaceHolder$title";
        string tableContent = "ctl00$ContentPlaceHolder$content";
        string tablePublish = "ctl00$ContentPlaceHolder$cbPublish";
        if (0 == (int)Session["GroupID"])
        {
            PublishLabel.Visible = false;
            cbPublish.Visible = false;
        }
        if (Session["OperationType"].ToString().Equals("New"))
        {

        }
        else if (Session["OperationType"].ToString().Equals("Edit"))
        {
            NewsDataClassesDataContext dc = new NewsDataClassesDataContext();
            var OtherInfos = from otherinfo in dc.OtherInfos
                             where otherinfo.OtherInfo_id.Equals(News_id)
                             select otherinfo;
            foreach (var otherinfo in OtherInfos)
            {
                otherinfo.Title = Request.Form[tableTitle];
                otherinfo.Content = Request.Form[tableContent];

                if (Request.Form[tablePublish] != null)
                {
                    otherinfo.isPublished = 1;
                }
                else { otherinfo.isPublished = 0;}
            }
            title.Value = Request.Form[tableTitle];
            content.Value = Request.Form[tableContent];
            if (Request.Form[tablePublish] != null) cbPublish.Checked = true;
            else cbPublish.Checked = false;
            dc.SubmitChanges();
            dc.Dispose();
        }
        Response.Redirect("../Admin/AdminHome.aspx");
    }
    protected void Return_Button_Click(object sender, EventArgs e)
    {
        this.RegisterClientScriptBlock("E", "<script language=javascript>history.go(-2);</script>");
    }
}
